package com.sphd.emanu.fragment.month;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.sphd.emanu.R;
import com.sphd.emanu.adapter.MyViewPagerAdapter;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.fragment.cardnum.CardNumFragment;
import com.sphd.emanu.fragment.time.TimeFragment;
import com.sphd.emanu.interfaces.CardFragmentLetter;
import com.sphd.emanu.interfaces.Letter;
import com.sphd.emanu.interfaces.LetterAtoF;
import com.sphd.emanu.mvp.MVPBaseFragment;
import com.sphd.emanu.pojo.MonthOrYearInfo;
import com.sphd.emanu.pojo.login.User;
import com.sphd.emanu.utils.GetTimeTask;
import com.tuyenmonkey.mkloader.MKLoader;
import com.wzutils.L;
import com.wzutils.TimeUtils;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 本月  上月 本年 的 按钮切换
 *
 * @Author Hu
 * @Time 2017/7/17 -- 15:48.
 * @Email 87683202@qq.com
 */

public class MonthFragment extends MVPBaseFragment<MonthContract.View, MonthPresenter> implements MonthContract.View, Letter, CardFragmentLetter {

    @BindView(R.id.go_back_img)
    ImageView goBackImg;
    @BindView(R.id.now_month_radio)
    RadioButton nowMonthRadio;
    @BindView(R.id.last_month_radio)
    RadioButton lastMonthRadio;
    @BindView(R.id.now_year_radio)
    RadioButton nowYearRadio;
    @BindView(R.id.radio_group_for_month_year)
    RadioGroup radioGroupForMonthYear;
    Unbinder unbinder;
    Bundle bundle;
    @BindView(R.id.financial_balance_text)
    TextView financialBalanceText;
    @BindView(R.id.financial_balance_hint)
    TextView financialBalanceHint;
    @BindView(R.id.income_text)
    TextView incomeText;
    @BindView(R.id.income_hint)
    TextView incomeHint;
    @BindView(R.id.spending_text)
    TextView spendingText;
    @BindView(R.id.spend_hint)
    TextView spendHint;
    @BindView(R.id.last_money_text)
    TextView lastMoneyText;
    @BindView(R.id.last_money_hint)
    TextView lastMoneyHint;
    @BindView(R.id.buttom_balance_linear)
    LinearLayout buttomBalanceLinear;
    @BindView(R.id.show_time_cardnum_tablayout)
    TabLayout tabs;
    @BindView(R.id.vp_Fragment_pager)
    ViewPager vpFragmentPager;
    @BindView(R.id.loading_mkloader)
    MKLoader loadingMkloader;
    private int getIdFrom = 0;
    User user;
    int state = 0;
    private List<Fragment> list_fragment;//定义要装fragment的列表
    CardNumFragment cardNumFragment;
    TimeFragment timeFragment;
    MyViewPagerAdapter myViewPagerAdapter;
    LetterAtoF letterAtoF;
    MonthOrYearInfo monthOrYearInfo = null;
    boolean timeOk = false;
    boolean cardOk = false;

    boolean isOnResum=false;

    public void setLetterAtoF(LetterAtoF letterAtoF) {
        this.letterAtoF = letterAtoF;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_month;
    }

    @Override
    protected void initView() {
        unbinder = ButterKnife.bind(this, rootView);
        bundle = getArguments();
        user = (User) bundle.getSerializable(Constant.USER_ENTITY);
        getIdFrom = bundle.getInt(Constant.R_ID);
        initRadioGroup();
        initFragment();
        setUserVisibleHint(true);
        initEvent();

    }


    private void initFragment() {
        list_fragment = new ArrayList<>();
        timeFragment = new TimeFragment();
        cardNumFragment = new CardNumFragment();
        bundle.putString(Constant.CLASS_NAME, MonthFragment.class.getName());
        timeFragment.setArguments(bundle);
        cardNumFragment.setArguments(bundle);
        timeFragment.setLetter(this);
        cardNumFragment.setCardFragmentLetter(this);
        list_fragment.add(timeFragment);
        list_fragment.add(cardNumFragment);
        String titles[] = {"按时间查看", "按账户查看"};
        myViewPagerAdapter = new MyViewPagerAdapter(context, getChildFragmentManager(), list_fragment, titles);
        tabs.setupWithViewPager(vpFragmentPager);
        vpFragmentPager.setAdapter(myViewPagerAdapter);
    }

    private void initRadioGroup() {
        switch (getIdFrom) {
            //用来判断是本月还是上月
            case R.id.balance_now_image:
                setRadioStyle(true, false, false);
                state = 1;
                break;
            case R.id.balance_lastmonth_image:
                setRadioStyle(false, true, false);
                state = 2;
                break;
        }
    }

    private void addTitleData() {
        mPresenter.getAllAccountData(setMapMessage());
    }

    public Map setMapMessage() {
        Map<String, String> map = new HashMap<>();
        map.put("oid", String.valueOf(user.getOid()));
        map.put("state", String.valueOf(state));
        return map;
    }

    private void initEvent() {

        radioGroupForMonthYear.setOnCheckedChangeListener(
                (group, checkedId) -> {
                    View viewById = radioGroupForMonthYear.findViewById(checkedId);
                    if (!viewById.isPressed()) {
                        return;
                    }
                    switch (checkedId) {
                        case R.id.now_month_radio:
                            setRadioStyle(true, false, false);
                            state = 1;
                            break;
                        case R.id.last_month_radio:
                            setRadioStyle(false, true, false);
                            state = 2;
                            break;
                        case R.id.now_year_radio:
                            setRadioStyle(false, false, true);
                            state = 3;
                            break;
                    }
                    allOkAddData();
                }

        );
    }


    private void setRadioStyle(boolean b, boolean b1, boolean b2) {
        nowMonthRadio.setBackground(context.getDrawable(b ? R.drawable.radio_check_true : R.drawable.radio_check_false));
        nowMonthRadio.setTextColor(ContextCompat.getColor(context, b ? R.color.colorPrimary : R.color.white));
        lastMonthRadio.setBackground(context.getDrawable(b1 ? R.drawable.radio_check_true : R.drawable.radio_check_false));
        lastMonthRadio.setTextColor(ContextCompat.getColor(context, b1 ? R.color.colorPrimary : R.color.white));
        nowYearRadio.setBackground(context.getDrawable(b2 ? R.drawable.radio_check_true : R.drawable.radio_check_false));
        nowYearRadio.setTextColor(ContextCompat.getColor(context, b2 ? R.color.colorPrimary : R.color.white));
    }

    @Override
    protected void initData() {


    }

    @Override
    public void setBean(MonthOrYearInfo monthOrYearInfo) {
        this.monthOrYearInfo = monthOrYearInfo;
        dissProgressDialog();
        financialBalanceText.setText(monthOrYearInfo.getAllBalance() + "");
        incomeText.setText(monthOrYearInfo.getAllCredit() + "");
        incomeHint.setText("本月收入");
        spendingText.setText(monthOrYearInfo.getAllDebit() + "");
        spendHint.setText("本月支出");
        lastMoneyText.setText(monthOrYearInfo.getAllPreviousBalance() + "");
        lastMoneyHint.setText("上月余额");
        financialBalanceHint.setText("当前余额" + monthOrYearInfo.getBeginDate() + "~" + monthOrYearInfo.getEndDate());

        timeFragment.swipeRefreshData(state,0,monthOrYearInfo.getBeginDate(), monthOrYearInfo.getEndDate());
        cardNumFragment.swipeRefreshData(state,monthOrYearInfo.getBeginDate(), monthOrYearInfo.getEndDate());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @OnClick(R.id.go_back_img)
    public void onViewClicked() {
        letterAtoF.finishActivity();
    }

    @Override
    public void readyed() {
        timeOk = true;
        allOkAddData();
    }

    private void allOkAddData() {
        if (timeOk && cardOk) {
            addTitleData();
        }
    }

    @Override
    public void readyedCardFragment() {
        cardOk = true;
        allOkAddData();
    }

    @Override
    public void onPause() {
        super.onPause();
        L.e("onPause");
        isOnResum=false;
        timeFragment.setOnResume(isOnResum);
    }

    @Override
    public void onResume() {
        super.onResume();
        L.e("onResume");
        isOnResum=true;
        timeFragment.setOnResume(isOnResum);
    }
}
